#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include 	<unistd.h>

#define		RESULT_PATH 		"/home/parisa/PARISA-NICOLA/trunk/rpaxos-config-4/results/"
#define		SCRIPT_PATH		 	"/home/parisa/PARISA-NICOLA/trunk/rpaxos-config-4/scripts/"
#define		CONFIG_PATH			"/home/parisa/PARISA-NICOLA/trunk/rpaxos-config-4/config.cfg"

#define		STRING_SIZE			1024
#define		NUM_EXPERIMENTS		30

#define		INTERVAL_STEP		500
#define		SUBMIT_STEP			1

int first_acceptor_node_ip, first_learner_node_ip, num_learners, num_acceptors, different_submit_per_wake;

void make_path(char original[STRING_SIZE], int id, char * final, char file_name[STRING_SIZE]);
void validate_input_parameters(int argc, char *argv[]);
//------------------------------------------------------------------------------------------
int main(int argc, char *argv[]) {
	validate_input_parameters(argc, argv);

	int folder_id;
	char alp_script[STRING_SIZE], lp_script[STRING_SIZE], alp_kill_script[STRING_SIZE], lp_kill_script[STRING_SIZE];

	int submit_per_wake, interval, j, diff_submit_per_wake, k, packet_size;

	submit_per_wake = 1;
	interval = 9000;
	folder_id = 1;
	packet_size = 205;

	strcpy(alp_kill_script, SCRIPT_PATH);
	sprintf(alp_kill_script, "%salp_kill.sh %d %d", SCRIPT_PATH, first_acceptor_node_ip, num_acceptors);

	strcpy(lp_kill_script, SCRIPT_PATH);
	sprintf(lp_kill_script, "%slp_kill.sh %d %d", SCRIPT_PATH, first_learner_node_ip, num_learners);
	//	while (folder_id <= NUM_EXPERIMENTS) {
	//	for (interval = 1000; interval < 20000; interval += 1000)
	{
		submit_per_wake = 1;
		j = 25;
		//		k = submit_per_wake;
		//	for (j = 1; j <= num_acceptors + num_learners; j++){

		int num_proposers = j;
		//		while (submit_per_wake < 100)
		{
			//			for (k = submit_per_wake; k <= (num_proposers * submit_per_wake); k++)
			{
				diff_submit_per_wake = 3;

				//				int total_num_packets = diff_submit_per_wake + submit_per_wake * (num_proposers - 1);
				//				double thr = total_num_packets * packet_size;
				//				thr = thr / (double) interval;
				//				thr = thr * 8;
				//				thr = thr / (1024 * 1024);
				//				thr = thr * 1000 * 1000;
				//
				//				if (thr > 250 || thr < 120) {
				//					printf("folder_id: %d 	num_proposers: %d  diff_submit_per_wake %d	packets: %d interval: %d ", folder_id, j, k, total_num_packets, interval);
				//					printf("expected sending throughput  %f\n", thr);
				//					break;
				//				}

				memset(alp_script, '\0', STRING_SIZE);
				memset(lp_script, '\0', STRING_SIZE);

				sprintf(alp_script, "%salp_script.sh %d %d %s %s %d %d %d %d %d %d", SCRIPT_PATH, first_acceptor_node_ip, num_acceptors, CONFIG_PATH, RESULT_PATH, interval, 30, submit_per_wake, folder_id, num_proposers, diff_submit_per_wake);
				sprintf(lp_script, "%slp_script.sh %d %d %s %s %d %d %d %d %d", SCRIPT_PATH, first_learner_node_ip, num_learners, CONFIG_PATH, RESULT_PATH, interval, submit_per_wake, folder_id, diff_submit_per_wake, num_proposers - num_acceptors);

				system(alp_script);
				system(lp_script);

				sleep(160);

				system(alp_kill_script);
				system(lp_kill_script);

				folder_id++;
			}
			submit_per_wake++;
		}

		//}
		//submit_per_wake += SUBMIT_STEP;
	}
	return 1;
}
//------------------------------------------------------------------------------------------
void make_path(char original[STRING_SIZE], int id, char *final, char file_name[STRING_SIZE]) {
	strcpy(final, original);
	char id_string[STRING_SIZE];

	sprintf(id_string, "%d", id);
	strcat(final, id_string);
	strcat(final, "/");
	strcat(final, file_name);
}
//------------------------------------------------------------------------------------------
void validate_input_parameters(int argc, char *argv[]) {
	if (argc != 5) {

		printf("Incompatible call. Please try again:\n"
			"<1. IP of first Acceptor.>\n"
			"<2. Number of acceptors.>\n"
			"<3. IP of first Learner.>\n"
			"<4. Number of Learners.>\n");

		exit(-1);

	} else {

		first_acceptor_node_ip = atoi(argv[1]);
		num_acceptors = atoi(argv[2]);

		first_learner_node_ip = atoi(argv[3]);
		num_learners = atoi(argv[4]);

	}
}

